package com.yaolaizaixian.merchant.utils;

import android.content.Context;
import android.util.Log;

import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.net.DES3Util;
import com.yaolaizaixian.merchant.net.SignUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/3.
 */
public class MyNetUtils {

    private static MyNetUtils instance;
    private static final String TAG = "MyNetUtils";
    private HashMap<String, String> msortedParams=new  HashMap<String, String>();

    private MyNetUtils() {

    }

    public static synchronized MyNetUtils getInstance() {
        if (instance == null) {
            instance = new MyNetUtils();
        }
        return instance;
    }

    public Map<String, String> getNetData(HashMap<String, String> sortedParams,Context mcontext) {

        String userId = SharedPreferencesUtil.getString(mcontext, "usersysno", "");
        String loginSign = SharedPreferencesUtil.getString(mcontext, "loginSign", "");
        //先清除掉，再赋值
        msortedParams.clear();
        msortedParams=sortedParams;

        try {

            if (null!=msortedParams)
            {
                msortedParams.put("yaolai_app_sysno", userId);//固定传参
                msortedParams.put("loginSign", loginSign);//固定传参
                msortedParams.put("access_token", BaseInfo.ACCESS_TOKEN);//固定传参
                String sign = SignUtils.sign(BaseInfo.SERVICEKEY, sortedParams);
                msortedParams = new HashMap<String, String>();
                msortedParams.put("appid", "600000");
                msortedParams.put("sign_type", "APP");
                msortedParams.put("sign", sign);

                for (Map.Entry<String, String> entry : sortedParams.entrySet()) {

                    msortedParams.put(entry.getKey(), URLEncoder.encode(DES3Util.encode(entry.getValue(), BaseInfo.SERVICEKEY)));
                }
            }
            else
            {
                Log.e(TAG,"参数为空");
            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return msortedParams;
    }

}